﻿using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp_SignalRClient
{
    public partial class Form1 : Form
    {
        private HubConnection _connection;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLinkService_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtServerUrl.Text))
            {
                MessageBox.Show("退无效地址");
                return;
            }
             SignalRConnectionAsync(txtServerUrl.Text.Replace(" ", ""));
            
        }

        private void SignalRConnectionAsync(string url)
        {
            var conBuilder = new HubConnectionBuilder();
            conBuilder.WithUrl(url);
            _connection = conBuilder.Build();
            _connection.ServerTimeout = TimeSpan.FromSeconds(8);
            _connection.HandshakeTimeout = TimeSpan.FromSeconds(10);

            CancellationToken tocken = new CancellationToken()
            {

            };
             _connection.StartAsync().Wait();

            _connection.Closed += SignalCloseAsync;


            _connection.On<object>("ReciveUpdataeAsync", msg =>
            {
                this.Invoke(new Action(() =>
                {
                    txtRecv.AppendText($"{DateTime.Now} recv message :{msg}");
                }));
            });
        }

        private Task SignalCloseAsync(Exception arg)
        {
            throw new NotImplementedException();
        }
    }
}
